//
//  HotVehicleTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/6.
//

import UIKit
import FSPagerView
class HotVehicleTableViewCell: UITableViewCell {
    var pagerView: FSPagerView!
    var dataArray: [VehicleLisetModel] = []
    var seaCarFlag: Bool = false
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .clear
        self.clipsToBounds = true
        self.selectionStyle = .none
        self.initPagerView()
    }
    
    func initPagerView(){
        
        // Create a pager view
        let trans = FSPagerViewTransformer(type: .linear)
        self.pagerView = FSPagerView(frame: CGRect(x: 0, y: 10, width: kScreenWidth, height: 195))
        pagerView.transformer = trans
        self.pagerView.dataSource = self
        self.pagerView.delegate = self
        self.pagerView.decelerationDistance = 1
        let width = (kScreenWidth - 50)
//        self.pagerView.automaticSlidingInterval = 3
        self.pagerView.interitemSpacing = 20
        self.pagerView.isInfinite = false//顺时针一直滑动
        self.pagerView.itemSize = CGSize(width: width, height: 195)
        self.pagerView.register(HotVehicleCell.self, forCellWithReuseIdentifier: "cell")
        self.contentView.addSubview(self.pagerView)
        
        self.pagerView.snp.makeConstraints { make in
            make.top.equalTo(10)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(195)
            make.bottom.equalTo(-10)
        }
    }
    
 
    func setDataArray(dataArray :[VehicleLisetModel]){
        self.dataArray = dataArray
        if dataArray.count > 1{
        self.pagerView.automaticSlidingInterval = 3
        self.pagerView.isInfinite = true//顺时针一直滑动
        }else{
            self.pagerView.isInfinite = false
        }
        self.pagerView.reloadData()
        self.updateConstraints()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }

}


extension HotVehicleTableViewCell: FSPagerViewDataSource,FSPagerViewDelegate{
    
     func numberOfItems(in pagerView: FSPagerView) -> Int {
         return self.dataArray.count
    
    }
    
     func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "cell", at: index) as! HotVehicleCell
        if self.dataArray.count > index{
            let model = self.dataArray[index]
            cell.setModel(model: model)
        }
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        
        if self.dataArray.count > index {
            let model = self.dataArray[index]
            let vc = VehicleDetailsViewController()
            vc.id = "\(model.id ?? 0)"
            vc.seaCarFlag = self.seaCarFlag
            self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
 
    
    func pagerViewDidEndDecelerating(_ pagerView: FSPagerView){
      
//        if self.dataArray.count > pagerView.currentIndex{
//            let model = self.dataArray[pagerView.currentIndex]
//            self.delegate.membershipLevelSelect(model: model, selectIndex: pagerView.currentIndex)
//        }
        print("滑动到了第\(pagerView.currentIndex)个row")
    }
    
}
